﻿namespace DotNetUtility.Common
{
    using System;
    /// <summary>
    /// To do logical operation on multiple boolean values.
    /// </summary>
    /// <example>
    ///     <code>
    ///         class Program
    ///         {
    ///             static void Main(string[] args)
    ///             {
    ///                 bool resultAnd
    ///                     = LogicalOperation.DoSequenceOfAnd(true, false);
    ///                 bool resultOr
    ///                     = LogicalOperation.DoSequenceOfOr(true, true);
    ///                 bool resultXOr
    ///                     = LogicalOperation.DoSequenceOfXOr(true, false, false, false);
    ///
    ///             }
    ///         }    
    ///     </code>
    /// </example>
    public static class LogicalOperation
    {
        /// <summary>
        /// It will Or on all the input values.
        /// </summary>
        /// <param name="sequenceOfBooleanValue">Sequence of boolean values</param>
        /// <returns>Outout of And operation</returns>
        /// <example>
        ///     <code>
        ///         bool resultOfAnd = LogicalOperation.DoSequenceOfAnd(true, false);
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static bool DoSequenceOfAnd(params bool[] sequenceOfBooleanValue)
        {
            bool andResult = true;

            Array.ForEach(sequenceOfBooleanValue,
                (item) =>
                {
                    andResult &= item;
                });
            return andResult;
        }

        /// <summary>
        /// It will Or on all the input values.
        /// </summary>
        /// <param name="sequenceOfBooleanValue">Sequence of boolean values</param>
        /// <returns>Ored output</returns>
        /// <example>
        ///     <code>
        ///         bool resultOfOr = LogicalOperation.DoSequenceOfOr(true, false);
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static bool DoSequenceOfOr(params bool[] sequenceOfBooleanValue)
        {
            bool orredResult = default(bool);

            Array.ForEach(sequenceOfBooleanValue,
                (item) =>
                {
                    orredResult |= item;
                });
            return orredResult;
        }

        /// <summary>
        /// It will Xor on all the input values.
        /// </summary>
        /// <param name="sequenceOfBooleanValue">Sequence of boolean values</param>
        /// <returns>Xored output</returns>
        /// <example>
        ///     <code>
        ///         bool resultOfXor = LogicalOperation.DoSequenceOfXOr(true, false);
        ///     </code>
        /// </example>
        /// Contributed by Mohammad Rahman
        public static bool DoSequenceOfXOr(params bool[] sequenceOfBooleanValue)
        {
            bool xoredResult = default(bool);

            Array.ForEach(sequenceOfBooleanValue,
                (item) =>
                {
                    xoredResult ^= item;
                });
            return xoredResult;
        }
    }
}
